import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Test {
    class MyStack {
        private Queue<Integer> s1;
        private Queue<Integer> s2;


        public MyStack() {
            s1 = new LinkedList<>();
            s2 = new LinkedList<>();

        }

        public void push(int x) {
            if(empty()) {
                s1.offer(x);
                return;
            }

            if(!s1.isEmpty()) {
                s1.offer(x);
            } else {
                s2.offer(x);
            }
        }

        public int pop() {
            if(empty()) {
                return -1;
            }
            if(s1.isEmpty()) {
                int a = s2.size();
                while(a - 1 != 0) {
                    s1.offer(s2.poll());
                    a--;
                }
                return s2.poll();
            } else {
                int a = s1.size();
                while(a - 1 != 0) {
                    s2.offer(s1.poll());
                    a--;
                }
                return s1.poll();
            }
        }

        public int top() {
            if(empty()) {
                return -1;
            }
            if(s1.isEmpty()) {
                int a = s2.size();
                int tem = 0;
                while(a != 0) {
                    tem = s2.poll();
                    s1.offer(tem);
                    a--;
                }
                return tem;
            } else {
                int a = s1.size();
                int tem = 0;
                while(a != 0) {
                    tem = s1.poll();
                    s2.offer(tem);
                    a--;
                }
                return tem;
            }
        }

        public boolean empty() {
            return s1.isEmpty() && s2.isEmpty();
        }
    }
    class MyQueue {

        private Stack<Integer> s1;
        private Stack<Integer> s2;

        public MyQueue() {
            s1 = new Stack<>();
            s2 = new Stack<>();

        }

        public void push(int x) {

            s1.add(x);

        }

        public int pop() {
            if(empty()) {
                return -1;
            }
            if(s2.empty()) {
                while(!s1.empty()) {
                    s2.add(s1.pop());
                }
            }
            return s2.pop();
        }

        public int peek() {
            if(empty()) {
                return -1;
            }
            if(s2.empty()) {
                while(!s1.empty()) {
                    s2.add(s1.pop());
                }
            }
            return s2.peek();
        }

        public boolean empty() {
            return s1.empty() && s2.empty();
        }
    }
}
